package com.cqut.forum.action;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cqut.forum.entity.User;
import com.cqut.forum.service.UserService;
import com.cqut.forum.util.FileHandle;

/**
 * 用户图片controller
 * @author yangxun
 */
@Controller
public class UserPhotoAction {
	@Resource
	private UserService userService;
	
	@RequestMapping("/user/user_photo")
	public String userPhoto(HttpServletRequest req) 
		throws IllegalStateException, IOException{
		User user = (User) req.getSession().getAttribute("user");
		if(user == null){
			return null;
		}
		
		MultipartHttpServletRequest request = (MultipartHttpServletRequest)req;
		String result = "";
		
		MultipartFile patch = request.getFile("file");
		//文件后缀
		String suffix = patch.getOriginalFilename().substring(patch.getOriginalFilename().lastIndexOf("."));  
		//文件名
		String fileName = user.getUserName()+suffix;  
		//判定上传的文件格式是否正确
		if ((".jpg.gif.bmp.jpeg.png".indexOf(suffix.toLowerCase()) != -1)) { 
			Integer fileSize = (int) patch.getSize() / 1024; 
			/** 
             * 如果文件小于10M，则上传文件，否则提示用户不能超过10M 
             */  
            if (fileSize <= 10240) {
            	String uploadPath = "/userPhoto";
            	System.out.println(uploadPath);  
                File file = new File(request.getSession()  
                        .getServletContext().getRealPath(""));  
                File userPhoto = new File(file.getParentFile().getPath()+"/user/userPhoto");
                System.out.println(userPhoto.getAbsolutePath()); 
                /** 
                 * 判读存储文件路是否存在，不存在则创建 
                 */  
                if (! userPhoto.exists()) {  
                	userPhoto.mkdirs();  
                    System.out.println("上传文件路径不存在，创建成功！");  
                }  
                /** 
                 * 文件开始上传到服务器上 
                 */  
                File img = new File(userPhoto.getAbsolutePath()+"\\"+fileName);
                patch.transferTo(img);
                FileHandle.createThumbnail(img.getPath(),0.3);
                //user.setUserPhoto(FileHandle.createThumbnail(img.getPath(),0.8));
                result = "/pages/user/user_control_photo.html";
            }
            else{
            	result = "请选择小于10M的图片！";
            }
		}
		else{
			result = "请选择.jpg.gif.bmp.jpeg.png格式的图片！";
		}
		user.setUserPhoto("../../../user/userPhoto/"+fileName);
		userService.update(user);
		req.getSession().setAttribute("user", user);
		return result ;
	}
	
	/***************get and set method*****************/
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
